﻿using System;
using System.Windows.Forms;
using MSTSCLib;
using System.IO;
using System.Collections.Generic;
using System.Runtime.InteropServices;


namespace RemoteDesctopManager
{
    public partial class FormChild : Form
    {
        public FormChild()
        {
            InitializeComponent();
        }

        public string Server = "";
        public string UserName = "";
        public string Password = "";
        public int Port = 0;
        public int DesktopWidth = 800;
        public int DesktopHeight = 600;
        public string NameForm = "";
        private TabControl tabCtrl = new TabControl();
        private TabPage tabPag = new TabPage();

        public TabPage TabPag
        {
            get
            {
                return tabPag;
            }
            set
            {
                tabPag = value;
            }
        }

        public TabControl TabCtrl
        {
            set
            {
                tabCtrl = value;
            }
        }

        private void FormChild_Load(object sender, EventArgs e)
        {
            this.Text = NameForm;
            try
            {
                int panelClientW = panel1.Width - panel1.ClientSize.Width;
                int panelClientH = panel1.Height - panel1.ClientSize.Height;
                rdpClient.DesktopHeight = DesktopHeight - panelClientH;
                rdpClient.DesktopWidth = DesktopWidth - panelClientW;
                rdpClient.AdvancedSettings2.RedirectDrives = true;
                rdpClient.AdvancedSettings2.RedirectPorts = true;
                rdpClient.AdvancedSettings2.SmartSizing = true;
                ((Control)rdpClient).DragEnter += new DragEventHandler(rdpClient_DragEnter);
                ((Control)rdpClient).DragDrop += new DragEventHandler(rdpClient_DragDrop);
                if (Port != 0) rdpClient.AdvancedSettings2.RDPPort = Port;
                rdpClient.Server = Server;
                rdpClient.UserName = UserName;
                IMsTscNonScriptable secured = (IMsTscNonScriptable)rdpClient.GetOcx();
                secured.ClearTextPassword = Password;
                rdpClient.Connect();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void rdpClient_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            string desktopShare = GetDesktopShare();
            if (String.IsNullOrEmpty(desktopShare))
            {
                MessageBox.Show(this, "A Desktop Share was not defined for this connection.\n" +
                    "Please define a share in the connection properties window (under the Local Resources tab)."
                    , "Terminals", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                SHCopyFiles(files, desktopShare);
            }
        }

        public String GetDesktopShare()
        {
            String desktopShare = "";
            //if (String.IsNullOrEmpty(desktopShare))
            //{
            //    desktopShare = Settings.DefaultDesktopShare.Replace("%SERVER%", rdpClient.Server)
            //                                                        .Replace("%USER%", rdpClient.UserName)
            //                                                        .Replace("%server%", rdpClient.Server)
            //                                                        .Replace("%user%", rdpClient.UserName);
            //}
            
            return desktopShare;
        }

        private void SHCopyFiles(string[] sourceFiles, string destinationFolder)
        {
            SHFileOperationWrapper fo = new SHFileOperationWrapper();
            List<string> destinationFiles = new List<string>();

            foreach (string sourceFile in sourceFiles)
            {
                destinationFiles.Add(Path.Combine(destinationFolder, Path.GetFileName(sourceFile)));
            }

            fo.Operation = SHFileOperationWrapper.FileOperations.FO_COPY;
            fo.OwnerWindow = this.Handle;
            fo.SourceFiles = sourceFiles;
            fo.DestFiles = destinationFiles.ToArray();
            fo.DoOperation();
        }

        private void rdpClient_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;
        }

        private void FormChild_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(rdpClient.Connected.ToString() == "1") rdpClient.Disconnect();

            this.tabPag.Dispose();
            if (!tabCtrl.HasChildren)
            {
                tabCtrl.Visible = false;
            }
        }

        private void FormChild_Activated(object sender, EventArgs e)
        {
            tabCtrl.SelectedTab = tabPag;

            if (!tabCtrl.Visible)
            {
                tabCtrl.Visible = true;
            }
        }

        private void FormChild_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;
        }

        private void FormChild_DragDrop(object sender, DragEventArgs e)
        {
            string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);

            foreach (string File in FileList)
                e.Effect = DragDropEffects.Copy;
        }
    }

}
